/**
 * @author : cyb1010
 * @date   : 2024-05-30 09:06:58
 * @file   : guess.cpp
 */
#include <bits/stdc++.h>
using namespace std;
#define fo(s) freopen(s ".in", "r", stdin), freopen(s ".out", "w", stdout)
#define fi first
#define se second
typedef double db;
typedef long double ldb;
typedef long long ll;
typedef unsigned long long ull;
void ckmn(int &x, int y) { x > y && (x = y); }
void ckmx(int &x, int y) { x < y && (x = y); }
const int N = 1e7 + 10, mod = 998244353;
const ldb eps = 1e-17;
int __, _, n, a[N];
ll qp(ll x, ll k, ll res = 1) {
    x %= mod, res %= mod;
    while (k) {
        if (k & 1) res = res * x % mod;
        x = x * x % mod, k >>= 1;
    }
    return res;
}
void work() {
    int _a, _b, _c, _M, _x, mn, mx = 0;
    cin >> n >> _x >> _a >> _b >> _c >> _M;
    _x %= _M, _a %= _M, _b %= _M, _c %= _M;
    fill(a + 1, a + 1 + _M, 0), mn = _M;
    auto gen = [&](int x) {
        return ((ll)x * x % _M * _a + (ll)x * _b + _c) % _M + 1;
    };
    for (int i = 1; i <= n; i++) a[_x = gen(_x)]++, ckmn(mn, _x), ckmx(mx, _x);
    if (mn == mx) return cout << "1 1\n", void();
    auto chk = [&](ldb p) {
        ldb r = 1;
        for (int i = mn; i <= mx; i++) {
            if (!a[i]) continue;
            ldb l = max(0.L, p * 2 - r);
            if (l > 1) return false;
            r = 1 - l - (a[i] > 1 ? p : 0);
            if (r < 0) return false;
        }
        return true;
    };
    ldb l = 0, r = 1, mid;
    while (r - l > eps) {
        (chk(mid = (l + r) / 2) ? l : r) = mid;
    }
    ldb val = 1;
    int ans = 0;
    for (int i = 1; i <= n * 2; i++) {
        ldb tmp = roundl(i * mid) / i;
        if (fabsl(tmp - mid) < val) val = fabsl(tmp - mid), ans = i;
    }
    printf("%.6Lf %d\n", mid, qp(ans, mod - 2, roundl(ans * mid)));
}
int main() {
    fo("guess");
    cin.tie(0)->sync_with_stdio(0);
    cin >> _;
    while (_--) work();
    cerr << clock() << endl;
    return 0 ^ __ ^ 0;
}